﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DnPakExplorer.Forms {
	public partial class CharacterSelectorForm : Form {

		public CharacterInfo SelectedCharacterInfo {
			get;
			private set;
		}

		public CharacterSelectorForm() {
			InitializeComponent();
		}

		public void SetCharacterInfos(CharacterInfo[] characters) {
			listView1.Items.Clear();
			foreach (CharacterInfo info in characters) {
				ListViewItem item = listView1.Items.Add(info.name);
				item.SubItems.Add(DnResourceUtil.GetJobName(info.job));
				item.SubItems.Add(info.level.ToString());
				item.SubItems.Add(info.heroLevel.ToString());
				item.Tag = info;
			}
		}

		private void listView1_MouseDoubleClick(object sender, MouseEventArgs e) {
			if (e.Button == System.Windows.Forms.MouseButtons.Left) {
				ListViewItem item = listView1.GetItemAt(e.X, e.Y);
				if (item != null) {
					SelectedCharacterInfo = item.Tag as CharacterInfo;
					DialogResult = System.Windows.Forms.DialogResult.OK;
					Close();
				}
			}
		}

		private void button2_Click(object sender, EventArgs e) {
			ListView.SelectedListViewItemCollection selects = listView1.SelectedItems;
			if (selects != null &&　selects.Count>0) {
				SelectedCharacterInfo = selects[0].Tag as CharacterInfo;
				DialogResult = System.Windows.Forms.DialogResult.OK;
			}
		}

		private void button1_Click(object sender, EventArgs e) {
			DialogResult = System.Windows.Forms.DialogResult.Cancel;
			Close();
		}

		private void button3_Click(object sender, EventArgs e) {
			CharacterEditorForm form = new CharacterEditorForm();
			if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
				try {
					DnPlayerUtil.CreateCharacter(form.CharacterInfo);
					SelectedCharacterInfo = form.CharacterInfo;
					SetCharacterInfos(DnPlayerUtil.AllCharacters.ToArray());
				} catch (Exception ex) {
					System.Windows.Forms.MessageBox.Show(ex.Message);
				}
			}
		}
	}
}
